//
//  XMMainViewController.swift
//  XMWeimo
//
//  Created by ios001 on 16/7/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class XMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tabBar = XMTabBar()
        tabBar.XMDelegate = self
        
        tabBar.closure = {[weak self] in
            print("按钮被点击了")
        
            // 创建view
            let composeView = XMComposeView()
            composeView.composeViewShow(self!)
            
//            self.presentViewController(UINavigationController(rootViewController: XMComposeViewController()), animated: true, completion: nil)
        }
        
        // 只读属性
//        self.tabBar = tabBar
        // 通过kvc 设置
        setValue(tabBar, forKey: "tabBar") // 别写错
        
        addChildViewController(XMHomeViewController(), title: "首页", imgName: "tabbar_home")
        addChildViewController(XMMessageViewController(), title: "消息", imgName: "tabbar_message_center")
        addChildViewController(XMDiscoverViewController(), title: "发现", imgName: "tabbar_discover")
        addChildViewController(XMProfileViewController(), title: "我", imgName: "tabbar_profile")
    }
    
    // MARK: 添加子控制器
    func addChildViewController(vc: UIViewController, title: String,imgName: String) {
        vc.title = title
        // 设置图片
        vc.tabBarItem.image = UIImage(named: imgName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        // 选择渲染方式
        vc.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        // 设置颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        // 添加子控制器
        addChildViewController(UINavigationController(rootViewController: vc))
    }
}


extension XMMainViewController: XMTabBarDelega {
    func composeButtonclick() {
        print("撰写按钮被点击了")
    }
}
